﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using System.IO;
using SES.CMS.DO;
using SES.CMS.BL;
using System.Net.Configuration;
using System.Text;

namespace SES.CMS
{
    public partial class BaoGia : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Download Báo giá - Văn Phòng Phẩm Mỹ Đình";
            lblInfo.Text = new sysConfigBL().Select(new sysConfigDO { ConfigID = 8 }).ConfigValue;
            if (Session["IDDN"] != null)
            {
                pnTT.Visible = false;
            }
            else
            {
                pnTT.Visible = true;
            }
        }

     
        public static String Address
        {
            get
            {
                SmtpSection cfg = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                return cfg.Network.UserName;
            }
        }
        public static String Pass
        {
            get
            {
                SmtpSection cfg = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                return cfg.Network.Password;
            }
        }
        public void sendsMail(String name, String bd, string title)
        {
            string i = DateTime.Now.ToString();
            sysConfigDO cmsConfig = new sysConfigDO();
            int id = 4;

            cmsConfig = new sysConfigBL().Select(new sysConfigDO { ConfigID = 4 });

            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.Credentials = new System.Net.NetworkCredential(Address, Pass);
            SmtpServer.Port = 587;
            SmtpServer.Host = "Smtp.gmail.com";
            SmtpServer.EnableSsl = true;
            MailMessage mail = new MailMessage();
            String addr = cmsConfig.ConfigValue.ToString();

            try
            {
                mail.From = new MailAddress(addr, name, System.Text.Encoding.UTF8);
                mail.To.Add(addr);
                mail.Subject = title;
                mail.Body = bd;
                mail.IsBodyHtml = true;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                lbLoi.Text = ex.ToString() + addr;
            }
        }
        protected void ButtonSend_Click(object sender, EventArgs e)
        {
            ccJoin.ValidateCaptcha(txtbv.Text);
            if (!ccJoin.UserValidated)
            {
                Response.Write("<script language='javascript'>alert('Mã bảo vệ chưa chính xác..!!..')</script>");
                return;
            }
            else
            {
                try
                {
                    sendsMail(txtTen.Text," Họ tên: " + txtTen.Text + "<br/> Email: " + txtEmail.Text + " <br /> SĐT: " + txtsdt.Text + "<br /> Đã download báo giá lúc vào lúc: " + DateTime.Now.ToString(), "Đownload báo giá!");
                    string fname = new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue;
                    hplBG.NavigateUrl = "/Media/" + fname;
                    divbg.Visible = true;
                    
                }
                catch (Exception ee)
                {
                    lbLoi.Text = ee.ToString();
                }
            }
        }


        private void DownloadFile(string fname, bool forceDownload)
        {
            string path = MapPath(fname);
            string name = Path.GetFileName(path);
            string ext = Path.GetExtension(path);
            string type = "";
            // set known types based on file extension  
            if (ext != null)
            {
                switch (ext.ToLower())
                {
                    case ".htm":
                    case ".html":
                        type = "text/HTML";
                        break;

                    case ".txt":
                        type = "text/plain";
                        break;

                    case ".doc":
                    case ".rtf":
                        type = "Application/msword";
                        break;
                    
                    case ".xls":
                    case ".xlsx":
                        type = "application/vnd.ms-excel";
                        break;

                    case ".pdf":
                        type = "application/pdf";
                        break;
                       
                  
                }
            }
            if (forceDownload)
            {
                Response.AppendHeader("content-disposition",
                    "attachment; filename=" + name);
            }
            if (type != "")
                Response.ContentType = type;
            Response.WriteFile(path);
            Response.End();
        }
    }
}
